﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using AMPLibrary.Collections;
using AMPLibrary.Media;

namespace MediaLibrary.Controls
{
    /// <summary>
    /// Interaction logic for SongTagEditorWindow.xaml
    /// </summary>
    public partial class SongTagEditorWindow : Window
    {
        ///
        private Collection<MediaFile> mediaToModify;

        /// <summary>
        /// 
        /// </summary>
        private SongTagEditorWindow()
        {
            InitializeComponent();
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mediaToModify"></param>
        public SongTagEditorWindow(Collection<MediaFile> mediaToModify)
        {
            InitializeComponent();

            if (mediaToModify.Count == 0)
            {
                this.DialogResult = true;
                this.Close();
            }

            this.Topmost = true;
            this.mediaToModify = new Collection<MediaFile>(mediaToModify.Distinct());

            if ((from m in mediaToModify select m.Tag.Title).Distinct().Count() > 1)
                this.songTitleTextBox.Text = "Multiple Titles";
            else
                this.songTitleTextBox.Text = (from m in mediaToModify select m.Tag.Title).ToArray()[0];

            if ((from m in mediaToModify select m.Tag.Album).Distinct().Count() > 1)
                this.albumTitleTextBox.Text = "Multiple Albums";
            else
                this.albumTitleTextBox.Text = (from m in mediaToModify select m.Tag.Album).ToArray()[0];

            if (mediaToModify.Count > 1)
                this.composerTextBox.Text += "Multiple Composers";
            else
            {
                foreach (string[] composers in (from m in mediaToModify select m.Tag.Composers))
                {
                    if (composers == null)
                        continue;

                    foreach (string composer in composers)
                        this.composerTextBox.Text += composer.Trim() + ";";
                }
            }

            if ((from m in mediaToModify select m.Tag.Conductor).Distinct().Count() > 1)
                this.conductorTextBox.Text = "Multiple Conductors";
            else
                this.conductorTextBox.Text = (from m in mediaToModify select m.Tag.Conductor).ToArray()[0];

            if ((from m in mediaToModify select m.Tag.AlbumArtist).Distinct().Count() > 1)
                this.albumArtistTextBox.Text = "Multiple Artists";
            else
                this.albumArtistTextBox.Text = (from m in mediaToModify select m.Tag.AlbumArtist).ToArray()[0];

            if ((from m in mediaToModify select m.Tag.Genre).Distinct().Count() > 1)
                this.genreTextBox.Text = "Multiple Genres";
            else
                this.genreTextBox.Text = (from m in mediaToModify select m.Tag.Genre).ToArray()[0];

            if ((from m in mediaToModify select m.Tag.Year).Distinct().Count() > 1)
                this.yearTextBox.Text = "Multiple Years";
            else
                this.yearTextBox.Text = (from m in mediaToModify select m.Tag.Year).ToArray()[0];

            if ((from m in mediaToModify select m.Tag.Track).Distinct().Count() > 1)
                this.trackTextBox.Text = "Multiple Track Numbers";
            else
                this.trackTextBox.Text = (from m in mediaToModify select m.Tag.Track).ToArray()[0].ToString();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void saveButton_Click(object sender, RoutedEventArgs e)
        {
            if (songTitleTextBox.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Please provide an Song Title.", "Error", MessageBoxButton.OK);
                return;
            }

            AMPLibrary.Media.MediaPlayer mediaPlayer = AMPLibrary.Media.MediaPlayer.GetMediaPlayer();
            mediaPlayer.StopForUpdate();

            foreach (MediaFile media in mediaToModify)
            {
                if (MediaTypes.IsValidMusicMediaType(media.Path))
                {
                    try
                    {
                        if(this.songTitleTextBox.Text.Trim() != "Multiple Titles")
                            media.Tag.Title = this.songTitleTextBox.Text.Trim();

                        if (this.albumTitleTextBox.Text.Trim() != "Multiple Albums")
                            media.Tag.Album = this.albumTitleTextBox.Text.Trim();

                        if (this.composerTextBox.Text.Trim() != "Multiple Composers")
                        {
                            List<string> composers = new List<string>();

                            foreach (string composer in this.composerTextBox.Text.Trim().Split(';'))
                            {
                                if (composer != null)
                                {
                                    if (composer.Trim() != string.Empty)
                                        composers.Add(composer.Trim());
                                }
                            }

                            media.Tag.Composers = composers.ToArray();
                        }

                        if (this.conductorTextBox.Text.Trim() != "Multiple Conductors")
                            media.Tag.Conductor = this.conductorTextBox.Text.Trim();

                        if (this.conductorTextBox.Text.Trim() != "Multiple Artists")
                            media.Tag.AlbumArtist = this.albumArtistTextBox.Text.Trim();

                        if (this.genreTextBox.Text.Trim() != "Multiple Genres")
                            media.Tag.Genre = this.genreTextBox.Text.Trim();

                        if (this.yearTextBox.Text.Trim() != "Multiple Years")                        
                            media.Tag.Year = this.yearTextBox.Text.Trim();

                        if(this.trackTextBox.Text.Trim() != "Multiple Track Numbers")
                        {
                            int outTrack = 0;

                            if(int.TryParse(this.trackTextBox.Text.Trim(), out outTrack))
                                media.Tag.Track = outTrack;
                        }

                        media.Tag.WriteTagToDisk();
                    }
                    catch
                    {
                        MessageBox.Show("Could not update " + media.Path, "Error", MessageBoxButton.OK);
                    }
                }
            }

            mediaPlayer.ResumeFromUpdate();

            this.DialogResult = true;
            this.Close();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }
    }
}
